require.config({
    baseUrl: 'js/common'
});
define(['common', 'pagin'], function(defaultStr, pagin) {
    var baseUrl = defaultStr();
    /*月份管理模块*/
    var month = new Vue({
        el: '#month-container',
        data: {
            years: [],
            months: [],
            dates: [{
                "helpMonth": 9,
                "helpYear": 2016
            }],
            selected: {
                "year": '',
                "month": ''
            },
            je: '',
            url: {
                'modifyMoney': baseUrl + '/mgr/je/add.do',
                'getMoney': baseUrl + '/mgr/je/get.do',
                'addMonth': baseUrl + '/usr/date/add.do',
                'delMonth': baseUrl + '/usr/date/remove.do',
                'getMonths': baseUrl + '/usr/date/getAll.do',
                'getJe': baseUrl + '/mgr/je/get.do'
            },
            maxJe: 0
        },
        methods: {
            /*初始化列表*/
            init: function() {
                var _self = this;
                // 获取当年的年月,设置select
                var myDate = new Date();
                var year = myDate.getFullYear();
                for (var i = -5; i < 5; i++) {
                    _self.years.push(year + i);
                }
                for (var i = 1; i <= 12; i++) {
                    _self.months.push(i);
                }
                /*获取所有年月，初始化已添加年月*/
                _self._getAllDates();
                /*获取最大金额*/
                _self._getJe();
            },
            /*像服务器添加一条新的数据,并更新列表*/
            addMonth: function() {
                var _self = this;
                _self.selected.year.toString();
                var selected = JSON.stringify(_self.selected);
                selected = {
                    "helpYear": _self.selected.year.toString(),
                    "helpMonth": _self.selected.month
                }
                $.ajax({
                    type: "POST",
                    url: _self.url.addMonth,
                    data: JSON.stringify(selected),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message)
                        setTimeout(_self._getAllDates, 1000);
                    }
                });
            },
            /*删除月份*/
            delete: function(el, id) {
                var _self = this;
                id = id.toString();
                var newUrl = _self.url.delMonth + '?' + 'id=' + id;
                $.ajax({
                    type: "POST",
                    url: newUrl,
                    // data: id,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message);
                        setTimeout(_self._getAllDates, 1000);
                    }
                })
            },
            /*设置金额*/
            setMoney: function() {
                var _self = this;
                var je = {
                    "je": parseInt(_self.je)
                }
                $.ajax({
                    type: "POST",
                    url: _self.url.modifyMoney,
                    data: JSON.stringify(je),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message)
                    }
                })
            },
            /*请求服务器，获取所有年月*/
            _getAllDates: function() {
                var _self = this;
                _self.dates = [];
                $.ajax({
                    type: "GET",
                    url: _self.url.getMonths,
                    dataType: "json",
                    success: function(result) {
                        $.each(result.body, function(i, value) {
                            _self.dates.push(value);
                        })
                    }
                })
            },
            /*获取金额上限*/
            _getJe: function() {
                var _self = this;
                $.ajax({
                    type: "POST",
                    url: _self.url.getJe,
                    dataType: "json",
                    success: function(result) {
                        _self.maxJe = result.body.je;
                    }
                })
            }
        }
    })
    month.init();
    return {
        dates: month.dates
    }
});